const db = require('../db/index');

// 添加一级部门
exports.addDepartment = (req, res) => {
	// 查询部门名称是否重复
	const sql = 'select * from department1 where department1=? and delete_state=0';
	db.query(sql, req.body.department1, (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('部门名称重复');
		const sql = 'insert into department1 set ?';
		db.query(sql, req.body, (err, results) => {
			if (err) return res.cc(err);
			res.cc('添加成功', 0);
		})
	})
}

// 添加二级部门
exports.addDepartmentSon = (req, res) => {
	// 查询同一部门内小组名称是否重复 同时满足两个条件，同一部门，同一名字
	const sql = 'select * from department2 where department_id=? and department2=?';
	db.query(sql, [req.body.department_id ,req.body.department2], (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('该部门小组名称重复');
		const sql = 'insert into department2 set ?'
		db.query(sql, req.body, () => {
			if (err) return res.cc(err);
			res.cc('添加成功', 0);
		})
	})
}

// 是否启用
exports.startDepartment = (req, res) => {
	const sql = 'update department1 set ? where department1_id=?';
	db.query(sql, [req.body, req.body.department1_id], (err, results) => {
		if (err) return res.cc(err);
		res.cc('状态改变成功', 0);
	})
}

// 删除一级部门
exports.deleteDepartment = (req, res) => {
	// 判断当前部门是否有员工
	const sql = 'select * from user where id_department1=?'
	db.query(sql, req.body.department1_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('当前部门内含有员工，无法删除该岗位')
		// 删除一级部门
		const sql = 'update department1 set ? where department1_id=?';
		db.query(sql, [req.body, req.body.department1_id], (err, results) => {
			if (err) return res.cc(err);
			res.cc('删除成功', 0);
		})
	})
}

// 删除二级部门
exports.deleteDepartmentSon = (req, res) => {
	const sql = 'select * from user where id_department2=?'
	db.query(sql, req.body.department2_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('当前部门内含有员工，无法删除该岗位');
		const sql = 'update department2 set ? where department2_id=?';
		db.query(sql, [req.body, req.body.department2_id], (err, results) => {
			if (err) return res.cc(err);
			res.cc('删除成功', 0);
		})
	})
}

// 批量删除部门
exports.deleteDepartmentList = (req, res) => {
	// 处理传递过来的数组
	let ids = '';
	const arr = req.body;
	for (let i = 0; i < arr.length; i++) {
		ids += "'" + arr[i] + "'" + ","
	}
	ids = ids.substr(0, ids.length - 1)
	// 批量查询部门里是否有员工的部门
	const sql = "select * from user where id_department1 in(" + ids + ")";
	db.query(sql, ids, (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('当前所选部门内含有员工，无法删除');
		const sql = "update department1 set delete_state=1 where department1_id in(" + ids + ")";
		db.query(sql, ids, (err, results) => {
			if (err) return res.cc(err);
			res.cc('删除成功', 0)
		})
	})
}

// 修改二级部门
exports.updateDepartment = (req, res) => {
	// 查询是否有重复内容
	const sql = 'select * from department1 where department1=?'
	db.query(sql, req.body.department1, (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('岗位名称重复，请重新输入');
		// 修改内容
		const sql = 'update department1 set ? where department1_id=?';
		db.query(sql, [req.body, req.body.department1_id], (err, results) => {
			if (err) return res.cc(err);
			res.cc('修改成功', 0);
		})
	})
}

// 修改二级部门
exports.updateDepartmentSon = (req, res) => {
	// 查询是否有重复内容
	const sql = 'select * from department2 where delete_state=0 and department2=? and department_id=?'
	db.query(sql, [req.body.department2, req.body.department_id], (err, results) => {
		if (err) return res.cc(err);
		if (results.length > 0) return res.cc('岗位名称重复，请重新输入');
		// 修改内容
		const sql = 'update department2 set ? where department2_id=?';
		db.query(sql, [req.body, req.body.department2_id], (err, results) => {
			if (err) return res.cc(err);
			res.cc('修改成功', 0);
		})
	})
}

// 查询部门
exports.getDepartment = (req, res) => {
	// 获取一级部门列表总数
	const sql = 'select count(*) from department1 where delete_state=0';
	db.query(sql, (err, results) => {
		if (err) return res.cc(err);
		const listNumber = results[0]['count(*)'];
		if (listNumber === 0) return res.cc('当前没有数据');
		const sql = 'select * from department1 where delete_state=0'
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			if (results.length === 0) return res.cc('没有相关数据')
			// 提取 results 里的字段
			let arr = results.map((item) => {
				return Object.assign(
					{},
					{ department1_id: item.department1_id },
					{ department1: item.department1 },
					{ user_id: item.user_id },
					{ add_date: item.add_date },
					{ state: item.state }
				)
			})
			// 将创建人id替换为创建人名字
			const sql = 'select * from user'
			db.query(sql, (err, results) => {
				if (err) return res.cc(err);
				for (let i = 0; i < arr.length ; i++) {
					for (let j = 0; j < results.length ; j++) {
						if (arr[i].user_id === results[j].id) {
							arr[i].user_id = results[j].nick_name
						}
					}
				}
				// 提取 arr 里 department1_id 的值并组成新数组
				const ids = arr.map(x => {return x.department1_id});
				// 先查出所有得用户数据，再从数组中，筛选个数
				const sql = "select * from user where state < 2";
				db.query(sql, (err, results) => {
					if (err) return res.cc(err);
					for (let i = 0; i < ids.length ; i++){
						let n = 0;
						for(let j = 0; j < results.length ; j++){
							if (results[j].id_department1 === ids[i]) {
								n++;
							}
						}
						Object.defineProperty(arr[i], 'user_number' , {
							value: n,
							writable:true,
							enumerable:true,
							configurable:true
						})
					};
					// 通过查出得一级部门id 查出未删除部门下所有子部门
					let idList = '';
					for (let i = 0; i < ids.length; i++) {
						idList += "'" + ids[i] + "'" + ","
					}
					idList = idList.substr(0, idList.length - 1)
					const sql = "select * from department2 where department_id in(" + idList + ") and delete_state=0";
					db.query(sql, (err, results) => {
						if (err) return res.cc(err)
						for (let i = 0; i < arr.length; i++) {
							let arrList = [];
							for (let j = 0; j < results.length; j++) {
								if (arr[i].department1_id === results[j].department_id){
									arrList.push(results[j]);
								}
							}
							Object.defineProperty(arr[i] ,'departmentSon', {
								value: arrList,
								writable:true,
								enumerable:true,
								configurable:true
							})
						}
						res.send({
							status: 0,
							message: '查询成功',
							data: arr,
							listNumber
						})
					})
				})
			})
		})
	})
}

// 选择框查询部门
exports.getDepartmentList = (req, res) => {
	const sql = 'select * from department1 where state=0 and delete_state=0';
	db.query(sql, (err, results) => {
		if (err) return res.cc(err);
		let arr = results.map((item) => {
				return Object.assign(
					{},
					{ department1_id: item.department1_id },
					{ department1: item.department1 },
				)
			})
		const ids = arr.map(x => {return x.department1_id});
		let idList = '';
		for (let i = 0; i < ids.length; i++) {
			idList += "'" + ids[i] + "'" + ","
		}
		idList = idList.substr(0, idList.length - 1)
		const sql = "select * from department2 where department_id in(" + idList + ") and delete_state=0";
		db.query(sql, (err, results)=>{
			if (err) return res.cc(err);
			let arrSon = results.map((item) => {
				return Object.assign(
					{},
					{ department2_id: item.department2_id },
					{ department2: item.department2 }
				)
			});
			for(let i = 0; i < arr.length; i++){
				let arrList = [];
				for(let j = 0; j < results.length; j++){
					if (arr[i].department1_id === results[j].department_id){
						arrList.push(arrSon[j]);
					}
				}
				Object.defineProperty(arr[i], 'departmentSon', {
					value: arrList,
					writable:true,
					enumerable:true,
					configurable:true
				})
			};
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			})
		})
	})
}